package com.example.lambda;

public class Java8Test {

    private static int i = 10;

    public static void main(String[] args) {
        /**
         * lambda表达式重要特征
         * 1、可选类型声明：不需要声明参数类型，编译器可以统一识别参数类别
         * 2、可选的参数圆括号：一个参数无需定义圆括号，但是多个参数需要定义圆括号
         * 3、可选的大括号：如果主体包含了一个语句，就不需要使用大括号
         * 4、可选的返回关键字：如果主体只有一个表达式返回值则编译器会自动返回值，大括号需要指明表达式返回了一个数值
         */

        int k = 1;
        Java8Test java8Test = new Java8Test();
        //类型声明
        MathOperation addition = (int a,int b) -> a + b;
        //不通类型声明
        MathOperation subtraction = (a,b) -> a - b;
        //大括号中的返回语句
        MathOperation multiplication = (int a,int b) -> {
            i = 1;
            System.out.println("===============i："+i);
            return a * b;
        };
        //没有大括号及返回语句
        MathOperation division = (int a,int b) -> a/b;
        System.out.println("10 + 5 = " + java8Test.operate(10, 5, addition));
        System.out.println("10 - 5 = " + java8Test.operate(10, 5, subtraction));
        System.out.println("10 x 5 = " + java8Test.operate(10, 5, multiplication));
        System.out.println("10 / 5 = " + java8Test.operate(10, 5, division));
        System.out.println("10 x i = " + java8Test.operate(10, i, multiplication));
        k=3;
        System.out.println("10 x k = " + java8Test.operate(10, k, multiplication));
        //k = 2;
        System.out.println("k = " + k);
        //不用括号
        GreetingService greetingService1 = message ->
                System.out.println("Hello "+message);
        //用括号
        GreetingService greetingService2 = (message) ->
                System.out.println("Hello2 "+message);

        greetingService1.sayMessage("Runoob");
        greetingService2.sayMessage("Google");
    }

    interface MathOperation{
        int operation(int a, int b);
    }

    interface GreetingService{
        void sayMessage(String message);
    }

    private int operate(int a,int b,MathOperation mathOperation){

        return mathOperation.operation(a,b);
    }
}
